/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package de.swta;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;

/**
 *
 * @author Jan Korreck
 */
public class CategoryManager {

    private static CategoryManager INSTANCE = new CategoryManager();

    private HashMap<Integer, Category> allCats = new HashMap<Integer, Category>();

    public static CategoryManager getInstance() {
        return INSTANCE;
    }

    public void addCategory(Category newCat) {


        if (!this.allCats.containsKey(newCat.getId())) {
            this.allCats.put(newCat.getId(), newCat);
        } else {
            System.out.println("____________ Mng allready contains" + newCat.getId());
        }
    }

    public void removeCategory(Category newCat) {
        this.allCats.remove(newCat);
    }

    public void removeCategory(Integer id) {
        if (allCats.containsKey(id)) {
            this.allCats.remove(id);
//            this.l.get(0).propertyChange(new PropertyChangeEvent(this, "remove", id, ""));
        }
    }

    public int getMaxID() {

        Iterator<Category> catIt = this.getAllCategories().iterator();

        int maxID = Integer.MIN_VALUE;

        while(catIt.hasNext()) {
            Category actCat = catIt.next();
            System.out.println("actCat: " + actCat.getName() + "ID: " + actCat.getId());
            maxID = Math.max(maxID, actCat.getId());
        }

        return maxID;
    }
    public Category getCategory(Integer id) {
        return this.allCats.get(id);
    }

    public boolean hasCategory(Integer catID) {
        return this.allCats.containsKey(catID);
    }

    public List<Category> getAllCategories() {
        return new ArrayList(this.allCats.values());
    }

    public List<String> getCategoryNames() {
        ArrayList<String> names = new ArrayList<String>();


        Iterator<Category> valIt = this.allCats.values().iterator();

        while(valIt.hasNext()) {
            names.add(valIt.next().getName());
        }

        return names;
    }

    public int getSize() {
        return this.allCats.size();
    }

    public List<Category> find(int first, int count, String sortProperty, boolean sortAsc) {

        List<Category> subList = getIndex(sortProperty, sortAsc).subList(first, first + count);


        return subList;
    }

    private List<Category> getIndex(String sortProperty, boolean sortAsc) {
        // hier müsste die DB gefragt werden und dementsprechend sortiert werden

        System.out.println("___________________________________ SortProperty: " + sortProperty);
        if (sortProperty == null) {
            return new ArrayList<Category>(this.allCats.values());

        } else if (sortProperty.equals("id")) {

            ArrayList<Category> tmpCat = new ArrayList<Category>(this.allCats.values());

            return this.sortByID(tmpCat, sortAsc);

        } else if (sortProperty.equals("name")) {

            return this.sortByName(new ArrayList<Category>(this.allCats.values()), sortAsc);

        }

        throw new RuntimeException("unknown sort option [" + sortProperty +
                "]. valid options: [id] , [name]");
    }

     private List<Category> sortByID(ArrayList<Category> cats, boolean asc) {

        if (asc) {
            Collections.sort(cats, new Comparator() {

                public int compare(Object o1, Object o2) {

                    return ((Category)o1).getId().compareTo(((Category)o2).getId());
                }
            });
        } else {
            Collections.sort(cats, new Comparator() {

                public int compare(Object o1, Object o2) {

                    return ((Category)o2).getId().compareTo(((Category)o1).getId());
                }
            });
        }
        return cats;
    }


    private List<Category> sortByName(ArrayList<Category> cats, boolean asc) {

        if (asc) {
            Collections.sort(cats, new Comparator() {

                public int compare(Object o1, Object o2) {

                    return ((Category)o1).getName().compareTo(((Category)o2).getName());
                }
            });
        } else {
            Collections.sort(cats, new Comparator() {

                public int compare(Object o1, Object o2) {

                    return ((Category)o2).getName().compareTo(((Category)o1).getName());
                }
            });
        }
        return cats;
    }
}
